﻿/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2009 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

namespace LucasCode.PublicTypes
{
    public class GameAccountCollection : System.Collections.CollectionBase
    {
        public GameAccountCollection()
        {
        
        }

        public GameAccount this[string type]
        {
            get
            {
                foreach (GameAccount g in List)
                    if (g.Type == type)
                        return g;
                //account type does not exist, create a new one
                GameAccount gnew = new GameAccount();
                gnew.Type = type;
                List.Add(gnew);
                return gnew;
            }
        }

        public void Add(GameAccount account)
        {
            if (account.Created.CompareTo(this[account.Type].Created) > 0)
            {
                this[account.Type].Value = account.Value;
                this[account.Type].Created = account.Created;
            }
        }
        
        public void Add(GameAccount[] accounts)
        {
            foreach (GameAccount g in accounts)
                Add(g);
        }
       
        public GameAccount[] ToArray()
        {
            GameAccount[] accounts = new GameAccount[List.Count];
            int i = 0;
            foreach (GameAccount g in List)
                accounts[i++] = g;
            return accounts;
        }

        public int IndexOf(string type, string value)
        {
            if (string.IsNullOrEmpty(value))
                return -1;
            foreach (GameAccount g in List)
                if (g.Type == type && g.Value == value)
                    return List.IndexOf(g);
            return -1;
        }

        public bool Contains(string type, string value)
        {
            if (string.IsNullOrEmpty(value))
                return false;
            foreach (GameAccount g in List)
                if (g.Type == type && g.Value == value)
                    return true;
            return false;
        }

        public bool Contains(string value)
        {
            if (string.IsNullOrEmpty(value))
                return false;
            foreach (GameAccount g in List)
                if (g.Value == value)
                    return true;
            return false;
        }
    }
}